package com.tuoyu.peopletest.utils;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 关于时间操作
 */
public class SCTimeUtils {
    private final static String TAG = "SCTimeUtils";
    private static SimpleDateFormat sdf01 = new SimpleDateFormat("hh:mm:ss");


    /**
     * 秒转换小时-分-秒analytics/util/DateUtil.java
     *
     * @param seconds 秒为单位 比如..600秒
     * @return 比如...2小时3分钟52秒
     */
    public static String secToTime(int seconds) {
        int hour = seconds / 3600;
        int minute = (seconds - hour * 3600) / 60;
        int second = (seconds - hour * 3600 - minute * 60);

        StringBuffer sb = new StringBuffer();
        if (hour > 0) {
            sb.append(hour + ":");
        }
        if (minute > 0) {
            sb.append(minute + ":");
        }
        if (second > 0) {
            sb.append(second);
        }
//        if (second > 0) {
//            sb.append(second + ":");
//        }
//        if (second == 0) {
//            sb.append("");
//        }
        return sb.toString();
    }

    public static String getMusicTime(long seconds) {
        return sdf01.format(seconds);
    }

    public static String getSecToTime02(int seconds) {
        int hour = seconds / 3600;
        int minute = (seconds - hour * 3600) / 60;
        int second = (seconds - hour * 3600 - minute * 60);

        StringBuffer sb = new StringBuffer();
        if (hour > 0) {
            if (hour < 10) {
                sb.append("0" + hour);
            } else {
                sb.append(hour);
            }
            sb.append(":");
        } else {
            sb.append("00:");
        }
        if (minute > 0) {
            if (minute < 10) {
                sb.append("0" + minute);
            } else {
                sb.append(minute);
            }
            sb.append(":");
        } else {
            sb.append("00:");
        }

        if (second > 0) {
            if (second < 10) {
                sb.append("0" + second);
            } else {
                sb.append(second);
            }
        } else {
            sb.append("00");
        }

        return sb.toString();
    }

    public static String getSecToTime03(long seconds) {
        seconds = seconds / 1000;
        long hour = seconds / 3600;
        long minute = (seconds - hour * 3600) / 60;
        long second = (seconds - hour * 3600 - minute * 60);

        StringBuffer sb = new StringBuffer();
        if (hour > 0) {
            if (hour < 10) {
                sb.append("0" + hour);
            } else {
                sb.append(hour);
            }
            sb.append(":");
        } else {
            sb.append("00:");
        }
        if (minute > 0) {
            if (minute < 10) {
                sb.append("0" + minute);
            } else {
                sb.append(minute);
            }
            sb.append(":");
        } else {
            sb.append("00:");
        }

        if (second > 0) {
            if (second < 10) {
                sb.append("0" + second);
            } else {
                sb.append(second);
            }
        } else {
            sb.append("00");
        }

        return sb.toString();
    }

    public static String getSecToTime04(int seconds) {
        int hour = seconds / 3600;
        int minute = (seconds - hour * 3600) / 60;
        int second = (seconds - hour * 3600 - minute * 60);

        StringBuffer sb = new StringBuffer();
        if (hour > 0) {
            if (hour < 10) {
                sb.append("0" + hour);
            } else {
                sb.append(hour);
            }
            sb.append(":");
        }

        if (minute > 0) {
            if (minute < 10) {
                sb.append("0" + minute);
            } else {
                sb.append(minute);
            }
            sb.append(":");
        } else {
            sb.append("00:");
        }

        if (second > 0) {
            if (second < 10) {
                sb.append("0" + second);
            } else {
                sb.append(second);
            }
        } else {
            sb.append("00");
        }

        return sb.toString();
    }

    /**
     *
     * @param seconds 毫秒
     * @return
     */
    public static String getSecToTime05(int seconds) {
        int time01 = seconds/1000;
        StringBuffer sb = new StringBuffer();
        int hour = time01 / 3600;
        int minute = (time01 - hour * 3600) / 60;
        int second = (time01 - hour * 3600 - minute * 60);
        int millisecond = (seconds - hour * 3600 - minute * 60 - second * 1000);


        if (hour > 0) {
            sb.append(hour + "小时");
        }

        if (minute > 0) {
            sb.append(minute + "分钟");
        }

        if (second > 0) {
            sb.append(second+"秒");
        }

        if (millisecond > 0) {
            sb.append(millisecond+"毫秒");
        }


        return sb.toString();
    }

    /**
     * 将int类型数字转换成时分秒毫秒的格式数据
     *
     * @param time long类型的数据
     * @return HH:mm:ss.SSS
     * @author zero 2019/04/11
     */
    public static String msecToTime(int time) {
        String timeStr = null;
        int hour = 0;
        int minute = 0;
        int second = 0;
        int millisecond = 0;
        if (time <= 0)
            return "00:00:00.000";
        else {
            second = time / 1000;
            minute = second / 60;
            millisecond = time % 1000;
            if (second < 60) {
                timeStr = "00:00:" + unitFormat(second) + "." + unitFormat2(millisecond);
            } else if (minute < 60) {
                second = second % 60;
                timeStr = "00:" + unitFormat(minute) + ":" + unitFormat(second) + "." + unitFormat2(millisecond);
            } else {// 数字>=3600 000的时候
                hour = minute / 60;
                minute = minute % 60;
                second = second - hour * 3600 - minute * 60;
                timeStr = unitFormat(hour) + ":" + unitFormat(minute) + ":" + unitFormat(second) + "."
                        + unitFormat2(millisecond);
            }
        }
        return timeStr;
    }

    public static String unitFormat(int i) {// 时分秒的格式转换
        String retStr = null;
        if (i >= 0 && i < 10)
            retStr = "0" + Integer.toString(i);
        else
            retStr = "" + i;
        return retStr;
    }

    public static String unitFormat2(int i) {// 毫秒的格式转换
        String retStr = null;
        if (i >= 0 && i < 10)
            retStr = "00" + Integer.toString(i);
        else if (i >= 10 && i < 100) {
            retStr = "0" + Integer.toString(i);
        } else
            retStr = "" + i;
        return retStr;
    }

    /**
     * 格式化时间
     *
     * @param time
     * @return
     */
    private static SimpleDateFormat sdf_for = new SimpleDateFormat("MM月dd日");

    public static String getFormatData(long time) {
        String format = sdf_for.format(time);
        return format;
    }

    /**
     * 格式化时间
     * 3/12 2020
     * target 24 以下低版本手机，在设置年月格式时，使用下述代码，必然崩溃。影响较大！！！值得反思
     * // 错误写法
     * SimpleDateFormat sdf = new SimpleDateFormat("YYYYMMdd");
     * <p>
     * // 正确写法
     * SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
     *
     * @param time
     * @return
     */
    private static SimpleDateFormat sdf_for02 = new SimpleDateFormat("MM/dd\n \nyyyy");

    public static String getFormatData02(long time) {
        String format = sdf_for02.format(time);
        return format;
    }

    /**
     * 时。大小写不同！！！小写的h是12小时制，大写的H是24小时制。HH和H区别在于是否有前导0
     */
    private static SimpleDateFormat getSdf_for03 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    public static String getFormatData03(long time) {
        String format = getSdf_for03.format(time);
        return format;
    }

    public static long getTimeFormatData03(String time) {
        try {
            Date parse = getSdf_for03.parse(time);
            return parse.getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0;
    }

    private static SimpleDateFormat getSdf_for04 = new SimpleDateFormat("MM/dd");

    public static String getFormatData04(long time) {
        String format = getSdf_for04.format(time);
        return format;
    }

    private static SimpleDateFormat getSdf_for06 = new SimpleDateFormat("yyyy-MM-dd HH:mm");

    public static String getFormatData06(long time) {
        String format = getSdf_for06.format(time);
        return format;
    }

    private static SimpleDateFormat getSdf_for07 = new SimpleDateFormat("yyyy_MM_dd_HH_mm");

    public static String getFormatData07(long time) {
        String format = getSdf_for07.format(time);
        return format;
    }

    private static SimpleDateFormat getSdf_for08 = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss");

    public static String getFormatData08(long time) {
        String format = getSdf_for08.format(time);
        return format;
    }

    private static SimpleDateFormat getSdf_for09 = new SimpleDateFormat("HH:mm:ss");

    public static String getFormatData09(long time) {
        String format = getSdf_for09.format(time);
        return format;
    }

    private static SimpleDateFormat getSdf_for09_01 = new SimpleDateFormat("HH:mm");

    public static String getFormatData09_01(long time) {
        String format = getSdf_for09_01.format(time);
        return format;
    }

    private static SimpleDateFormat getSdf_for10 = new SimpleDateFormat("yyyyMMdd");

    public static String getFormatData10(long time) {
        String format = getSdf_for10.format(time);
        return format;
    }

    private static SimpleDateFormat getSdf_for11 = new SimpleDateFormat("dd/MM/yyyy");

    public static String getFormatData11(long time) {
        String format = getSdf_for11.format(time);
        return format;
    }


    private static SimpleDateFormat getSdf_for12 = new SimpleDateFormat("yyyy.MM.dd");

    public static String getFormatData12(long time) {
        String format = getSdf_for12.format(time);
        return format;
    }


    public static long getParseStr11(String timeStr) {
        try {
            Date parse = getSdf_for11.parse(timeStr);
            return parse.getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0;
    }
    private static SimpleDateFormat getSdf_for11_01 = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
    public static long getSdf_for11_01(String timeStr) {
        try {
            Date parse = getSdf_for11_01.parse(timeStr);
            return parse.getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 解析字符串
     *
     * @return
     */
    public static long getParseStr(String timeStr) {
        try {
            Date parse = getSdf_for03.parse(timeStr);
            return parse.getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 获取一天事件的开始,结束
     *
     * @return
     */
    private static SimpleDateFormat getSdf_date = new SimpleDateFormat("yyyy-MM-dd");

    public static long[] getDateTimeFirstAndLast() {
        long[] dateData = new long[2];
        try {
            long current = System.currentTimeMillis();
            String format = getSdf_date.format(current);

            Date parse = getSdf_date.parse(format);

            dateData[0] = parse.getTime();
            // 86399000 是一天的毫秒数 23:59:59 *1000
            dateData[1] = parse.getTime() + 86399000;
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return dateData;
    }

    /**
     * 获取一年, 时间的开始,结束
     *
     * @return
     */
    public static long[] getYearTimeFirstAndLast(String year) {
        long[] dateData = new long[2];
        try {
            String firstStr = year + "-01-01 00:00:00";
            //SCLogUtils.e(TAG,"开始firstStr="+firstStr);
            Date firstParse = getSdf_date.parse(firstStr);


            String lastStr = year + "-12-31 23:59:59";
            //SCLogUtils.e(TAG,"结束lastStr="+lastStr);
            Date lastStrParse = getSdf_date.parse(lastStr);

            dateData[0] = firstParse.getTime();
            dateData[1] = lastStrParse.getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return dateData;
    }
}
